import { defHttp } from '/@/utils/http/axios';
import { Modal } from 'ant-design-vue';

enum Api {
  list = '/api-upms/sys/quartzJob/list',
  save = '/api-upms/sys/quartzJob/saveOrUpdate',
  edit = '/api-upms/sys/quartzJob/saveOrUpdate',
  delete = '/api-upms/sys/quartzJob/delete',
  get = '/api-upms/sys/quartzJob/queryById',
  switch = '/api-upms/sys/quartzJob/switch',
  startAll = '/api-upms/sys/quartzJob/startAll',
  stopAll = '/api-upms/sys/quartzJob/stopAll',
  resume = '/api-upms/sys/quartzJob/resume',
  pause = '/api-upms/sys/quartzJob/pause',
  run = '/api-upms/sys/quartzJob/run',
  checkCronExpressionIsValid = '/api-upms/sys/quartzJob/checkCronExpressionIsValid',
  queryCronExpression = '/api-upms/sys/quartzJob/queryCronExpression',
  exportXlsUrl = '/api-upms/sys/quartzJob/exportXls',
  importExcelUrl = '/api-upms/sys/quartzJob/importExcel',
  deleteBatch = '/api-upms/sys/quartzJob/deleteBatch',
  checkQuartz = '/api-upms/sys/quartzJob/checkQuartz',
  logList = '/api-upms/sys/quartzJobLog/list',
}

/**
 * 导出api
 */
export const getExportUrl = Api.exportXlsUrl;
/**
 * 导入api
 */
export const getImportUrl = Api.importExcelUrl;
/**
 * 查询任务列表
 * @param params
 */
export const getQuartzList = (params) => {
  return defHttp.get({ url: Api.list, params });
};

/**
 * 保存或者更新任务
 * @param params
 */
export const saveOrUpdateQuartz = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params });
};

/**
 * 查询任务详情
 * @param params
 */
export const getQuartzById = (params) => {
  return defHttp.get({ url: Api.get, params });
};

/**
 * 删除任务
 * @param params
 */
export const deleteQuartz = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.delete, data: params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};

/**
 * 批量删除任务
 * @param params
 */
export const batchDeleteQuartz = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

/**
 * 全部启动
 * @param params
 */
export const startAllJob = (handleSuccess) => {
  return defHttp.put({ url: Api.startAll }).then(() => {
    handleSuccess();
  });
};

/**
 * 全部暂停
 * @param params
 */
export const switchJob = (params, handleSuccess) => {
  return defHttp.put({ url: Api.switch, params }).then(() => {
    handleSuccess();
  });
};

/**
 * 全部暂停
 * @param params
 */
export const stopAllJob = (handleSuccess) => {
  return defHttp.put({ url: Api.stopAll }).then(() => {
    handleSuccess();
  });
};

/**
 * 启动
 * @param params
 */
export const resumeJob = (params, handleSuccess) => {
  return defHttp.put({ url: Api.resume, params }).then(() => {
    handleSuccess();
  });
};

/**
 * 暂停
 * @param params
 */
export const pauseJob = (params, handleSuccess) => {
  return defHttp.put({ url: Api.pause, params }).then(() => {
    handleSuccess();
  });
};

/**
 * 立即执行
 * @param params
 */
export const executeImmediately = (params, handleSuccess) => {
  return defHttp.put({ url: Api.run, params }).then(() => {
    handleSuccess();
  });
};

/**
 * 校验cron表达式是否有效
 * @param params
 */
export const checkCronExpressionIsValid = (params, handleSuccess) => {
  return defHttp.put({ url: Api.checkCronExpressionIsValid, params }).then(() => {
    handleSuccess();
  });
};

/**
 * 查询cron表达式近5次的执行时间
 * @param params
 */
export const queryCronExpression = (params, handleSuccess) => {
  return defHttp.put({ url: Api.queryCronExpression, params }).then(() => {
    handleSuccess();
  });
};

/**
 * 检测定时任务
 * @param params
 */
export const checkQuartz = () => {
  return defHttp.get({ url: Api.checkQuartz }, { isTransformResponse: false });
};

/**
 * 查询任务日志列表
 * @param params
 */
export const getQuartzLogList = (params) => {
  return defHttp.get({ url: Api.logList, params });
};
